// pages/detail/index.js
import { getZ } from "../../http/interface.js"
const bgMusic = wx.getBackgroundAudioManager();
const { formatAudio, timestr2sec } = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo:"",
    rule:"",
    topic:"",
    title:"",
    content:"",
    center:"",
    audio_time:"",
    currentTime:'',
    intro_audio:"",
    over:false,
    maxTime: '', //总时长（字符串）
    max: '', //总时长（秒）
    playing: false, //正在播放,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNotice();
  },
  getNotice(){
    getZ().then(res=>{
      this.setData({
        logo:res.data.logo,
        intro_audio:res.data.intro_audio,
        rule:res.data.rule,
        center:res.data.center,
        topic:res.data.topic,
        title:res.data.title,
        content:res.data.content,
        audio_time:res.data.audio_time,
        currentTime: formatAudio(0),
        offest:0,
        max: timestr2sec(res.data.audio_time),
        maxTime: formatAudio(timestr2sec(res.data.audio_time))
      })
    })
    this.audioInit();
  },
//音频初始化
audioInit() {
  var that = this;

  bgMusic.onPlay(() => {
    // console.log('详情页开播了');
    bgMusic.duration //类似初始化-必须触发-不触发此函数延时也获取不到！！！！！！！！！！！！！！！

    wx.setStorage({
      key: 'bjaudioId',
      data: that.data.intro_audio,
    })
    setTimeout(function () {
      that.setData({
        playing: true,
        // 获取时长
        maxTime: formatAudio(Math.round(bgMusic.duration)),
      })
    }, 100) //这里设置延时获取
  });

  bgMusic.onPause(() => {
    // console.log('详情页暂停了');
    that.setData({
      playing: false,
    });
  });

  bgMusic.onTimeUpdate(() => {
    const bjaudioId = wx.getStorageSync('bjaudioId');
   
    if (!bgMusic.paused) {

      if (bgMusic.src == that.data.intro_audio) {
        console.log(bgMusic.currentTime)
        that.setData({
          playing: true,
          offset: Math.round(bgMusic.currentTime),
          over:false,
          currentTime: formatAudio(Math.round(bgMusic.currentTime)),
          maxTime: formatAudio(Math.round(bgMusic.duration))
        });
      } else {
        bgMusic.stop()
      }
    }
  });

  bgMusic.onEnded(() => {
    // console.log('详情页播完了');

    const bjaudioId = wx.getStorageSync('bjaudioId');

    that.setData({
      playing: false,
      offset: 0,
      over: true,
    });

    wx.removeStorage({
      key: 'bjaudioId',
      success(res) {
        console.log(res)
      }
    })
  });

  bgMusic.onStop(() => {
    // console.log('详情页关闭了');
    that.setData({
      playing: false,
      over: true,
    });
  });

},

play() { //播放讲解词
  var that = this;
  var bjaudioId = wx.getStorageSync('bjaudioId');
  if (that.data.playing) {
    bgMusic.pause();
  } else {
    
     //播放完成后需要再赋值一次才能再播放
        bgMusic.string = '滨海科技馆';
        bgMusic.src = that.data.intro_audio;
        bgMusic.title = that.data.title;
        bgMusic.coverImgUrl = that.data.logo; //封面图 URL，用于做原生音频播放器背景图。
    
        bgMusic.play();
      
  };
},
sliderChange(e) {
  var that = this;
  if (e.detail.value) {
      if (!that.data.playing) {
          bgMusic.string = '滨海科技馆';
          bgMusic.src = that.data.intro_audio;
          bgMusic.title = that.data.title;
          bgMusic.coverImgUrl = that.data.logo;
          wx.setStorage({
              key: 'bjaudioId',
              data: that.data.exhibit,
          })
          setTimeout(function() {
              bgMusic.seek(e.detail.value);
          }, 100)
      } else {
          bgMusic.seek(e.detail.value);
      }
  }
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})